package fun.flyee.android.pengshan.acts;

import android.os.AsyncTask;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.webkit.CookieManager;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.ImageView;

import com.amap.api.location.AMapLocationClient;

import java.io.File;

import butterknife.BindView;
import butterknife.OnClick;
import fun.flyee.pengshan.social.BuildConfig;
import fun.flyee.pengshan.social.R;
import fun.flyee.android.pengshan.Storage;
import fun.flyee.android.pengshan.utils.AppTimer;
import fun.flyee.android.pengshan.utils.Files;
import fun.flyee.android.pengshan.utils.Logger;
import fun.flyee.android.pengshan.utils.Systems;
import fun.flyee.android.pengshan.utils.UIUncompresser;
import fun.flyee.android.pengshan.widgets.Tips;
import kotlin.text.StringsKt;

public class SplashActivity extends BaseActivity {

    private static final int STAY_DELAY = 2000;
    private static final int STAY_TICK = 1000;

    @BindView(R.id.image)
    public ImageView imageView;

    @BindView(R.id.web_view)
    public WebView webView;

    protected int layoutId() {
        return R.layout.act_splash;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Systems.hideSystemStatusBar(this);
        Systems.hideSystemNavBar(this);
        AMapLocationClient.updatePrivacyShow(this, true, true);
        AMapLocationClient.updatePrivacyAgree(this, true);
        needClearCache();
        jumpByTimer();
        readyUI();
    }

    private void readyUI() {
        new AsyncTask() {

            @Override
            protected Object doInBackground(Object[] objects) {
                return UIUncompresser.exec(SplashActivity.this);
            }

            @Override
            protected void onPostExecute(Object obj) {
                if (obj instanceof Boolean && !((Boolean) obj)) {
                    Tips.toast("文件加载失败");
                }
            }
        }.execute();
    }

    private void jumpByTimer() {
        AppTimer.begin(STAY_DELAY, STAY_TICK, () -> {
            if (TextUtils.isEmpty(Storage.get(Storage.TOKEN)) || Storage.getUser() != null) {
                Router.login(this);
            } else {
                Router.main(this);
            }
            this.finish();
        });
    }

    private void needClearCache() {
        int lastVersion = Storage.getLastVersion();
        if (lastVersion == 0 || lastVersion != BuildConfig.VERSION_CODE) {
            clearCache();
            Storage.setCurrentVersion(BuildConfig.VERSION_CODE);
        }
    }

    private void clearCache() {
        Logger.i("start clear webview cache");
        webView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
        webView.getSettings().setAppCacheEnabled(false);
        webView.clearCache(true);
        webView.clearHistory();
        webView.clearFormData();
        CookieManager cm = CookieManager.getInstance();
        cm.removeSessionCookies(null);
        cm.removeAllCookies(null);
        try {
            deleteDatabase("webview.db");
            deleteDatabase("webviewCache.db");
        } catch (Exception e) {
            e.printStackTrace();
        }
        try {
            File file = this.getApplicationContext().getCacheDir().getAbsoluteFile();
            Files.deleteChildrenFiles(file);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
